<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>出勤表</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--列表显示-->
<table id="attendanceTable" lay-filter="attendanceFilter"></table>
<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>-->
    </div>
</script>

<!--部门添加form-->
<form id="attendanceAddForm" class="layui-form" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">员工编号</label>
        <div class="layui-input-block">
            <input type="text" name="attendanceTopType" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出勤时间</label>
        <div class="layui-input-block">
            <input type="text" name="attendanceName" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="attendanceAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<!--部门更新form-->
<form id="attendanceUpdateForm" class="layui-form" lay-filter="attendanceUpdateFilter" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="attendanceId">

    <div class="layui-form-item">
        <label class="layui-form-label">员工编号</label>
        <div class="layui-input-block">
            <input type="text" name="userId" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出勤时间</label>
        <div class="layui-input-block">
            <input type="text" name="attendanceTime" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <!--<div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="text" name="orderNum" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>-->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="attendanceUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--js 代码-->
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery','form'], function(){
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        var form = layui.form;
        //部门列表js
        let attendanceTable = table.render({
            elem: '#attendanceTable'
            ,toolbar: '#toolbarHead'
            ,url: '/attendance/queryAll' //数据接口
            ,page: true //开启分页
            ,cellMinWidth: 80
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'} //开启复选框
                    ,{field: 'userId', title: '员工编号', sort: true, fixed: 'left'}
                    ,{field: 'attendanceTime', title: '出勤时间', sort: true}
                    ,{fixed: 'right', align:'center', toolbar: '#barRowEdit'}
                ]
            ]
        });

        //行编辑工具栏监听事件
        table.on('tool(attendanceFilter)', function(obj){
            //获取当前行数据
            var data = obj.data;
            //==不匹配类型  1=='1' true      ==== 匹配类型  1==='1' false
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/attendance/deleteById",
                        type:"DELETE",
                        data:{attendanceId:data.attendanceId},
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //重新加载数据
                                attendanceTable.reload();
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                form.val("attendanceUpdateFilter",data);
                layer.open({
                    type: 1,
                    area:['400px', '250px'],
                    content: $('#attendanceUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //头部工具栏监听事件
        table.on('toolbar(attendanceFilter)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id);
            let  checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    // layer.msg('添加');
                    //重置form表单
                    // $("#attendanceAddForm").reset();
                    document.getElementById("attendanceAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['400px', '220px'],
                        content: $('#attendanceAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    //layer.msg('删除');
                    //layer.msg(JSON.stringify(checkData));
                    //判断是否选中行
                    if(checkData.length==0){
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('真的删除行么', function(index) {
                        //获取所有选中行id
                        let attendanceIds = "";
                        for (let i = 0; i < checkData.length; i++) {
                            attendanceIds += checkData[i].attendanceId + ",";
                        }
                        ;
                        attendanceIds = attendanceIds.substr(0, attendanceIds.length - 1);
                        layer.msg(attendanceIds);
                        //调用批量删除
                        $.ajax({
                            url: "/attendance/deleteByIds",
                            type: "DELETE",
                            data: {attendanceIds: attendanceIds},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    attendanceTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':

                    if(checkData.length!=1){
                        layer.msg('编辑只能选中一行');
                        return;
                    }
                    // alert(JSON.stringify(checkData));
                    form.val("attendanceUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['400px', '220px'],
                        content: $('#attendanceUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //添加监听事件
        form.on('submit(attendanceUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/attendance/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        attendanceTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });
        //更新监听事件
        form.on('submit(attendanceAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/attendance/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        attendanceTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });

    });
</script>
</body>
</html>